package com.anxin.registerManagement.util;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * @author anxin
 * @date 2022/4/6 20:09
 */
public class EmailUtil {
    /**
     * 发送QQ邮箱
     *
     * @param sendUser         发送用户
     * @param sendUserPassword 发送用户16位授权码
     * @param receiveUser      接收用户
     * @param title            主题
     * @param content          内容
     * @throws Exception 发送异常
     */
    public static void sendEmail(String sendUser, String sendUserPassword, String receiveUser, String title, String content) throws Exception {
        Properties properties = System.getProperties();
        properties.setProperty("mail.smtp.host", "smtp.qq.com");
        properties.put("mail.smtp.auth", "true");
        Session session = Session.getDefaultInstance(properties, new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(sendUser, sendUserPassword);
            }
        });
        try {
            MimeMessage mimeMessage = new MimeMessage(session);
            mimeMessage.setFrom(new InternetAddress(sendUser));
            mimeMessage.addRecipient(Message.RecipientType.TO,
                    new InternetAddress(receiveUser));
            mimeMessage.setSubject(title);
            mimeMessage.setText(content);
            Transport.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }

    }
}
